﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Enum;

public partial class Pages_PlaceInsert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDdlState();
        } 
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        Place place = new Place();
        PlaceController placeController = new PlaceController();
        City city = null;
        try
        {
            city = new City();
            city.Id= Convert.ToInt32(ddlCity.SelectedValue);
            place.ZipCode = Convert.ToInt32(txbZip.Text);
            place.Address = Convert.ToString(txbAddress.Text);
            place.Complement = Convert.ToString(txbComplement.Text);
            place.Number = Convert.ToInt32(txbNumber.Text);
            place.City = city;
            if (placeController.InsetPlace(place) == EnumGenericState.GENERIC_STATE_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Local cadastrado com sucesso!');</script>");
                ClearFields();
                ClearDdlCity();
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante o cadastro, por favor tente novamente!');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro ao criar o local: <br>" + ex.Message + ".');</script>");
            place = null;
            placeController = null;
        }
    }
    private void FillDdlState()
    {
        StateController stateController = new StateController();
        ddlState.DataSource = stateController.GetStates();
        ddlState.DataTextField = "Acronym";
        ddlState.DataValueField = "Code";
        ddlState.DataBind();
        ddlState.Items.Insert(0, "Selecione");
    }

    private void FillDdlCity()
    {
        CityController cityController = new CityController();
        State state = new State();
        if (Convert.ToInt32(ddlState.SelectedIndex)!=0)
        {
            state.Code = Convert.ToInt32(ddlState.SelectedValue);
            ddlCity.DataSource = cityController.listCityByState(state);
            ddlCity.DataTextField = "Description";
            ddlCity.DataValueField = "Id";
            ddlCity.DataBind();
            ddlCity.Items.Insert(0, "Selecione");
        }
        else
        {
            ClearDdlCity();
        }

    }
    private void ClearFields()
    {
        txbAddress.Text = "";
        txbComplement.Text = "";
        txbZip.Text = "";
        txbNumber.Text = "";
        for (int i = 0; i < ddlState.Items.Count; i++)
        {
            ddlState.Items[i].Selected = false;
        }
        ddlState.Items[0].Selected=true;

    }

    private void ClearDdlCity()
    {
        for (int i = 0; i < ddlState.Items.Count; i++)
        {
            ddlState.Items[i].Selected = false;
        }
        ddlState.Items[0].Selected = true;
        ddlCity.DataSource = "";
        ddlCity.DataBind();
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDdlCity();
    }
    
}